Kwik.Page.include("Kwik.Ui.Controls.Control.js");

/**
* Control utility
* @singleton
* @requires Kwik.Ui.Controls.Control
*/
Kwik.Ui.Controls.Utility =
{
	/** Get controls from the HTMLDocument by element, element.id or pointer @type Kwik.Ui.Controls.Control */
	getControl: function(element, controlClass)
	{
		if(Kwik.Type.isNumber(element) && Kwik.ObjectRegistry.objects[element])
		{
			Kwik.Page.log.write("Kwik.Controls.Utility.getControl: Found control by pointer.");
			return Kwik.ObjectRegistry.objects[element];
		}
		
		if(Kwik.Type.isString(element))
		{
			Kwik.Page.log.write("Kwik.Controls.Utility.getControl: Getting element by id.");
			element = document.getElementById(element);}
			if(element == null){throw new Error("Kwik.Controls.Utility.getControl: Element not found.");}
		}

		if(Kwik.Type.isElement(element))
		{
			Kwik.Page.log.write("Kwik.Controls.Utility.getControl: Initializing new control with element.");
			if(!Kwik.Type.isString(controlClass)){ controlClass="Kwik.Ui.Controls.Control";}

			var ctl = null;
			try
			{
				eval("ctl = new " + controlClass();");
			}
			catch(x)
			{
				throw new Error("Kwik.Controls.Utility.getControl: Failed initialize class '" + controlClass + "', reason: " + x.description);
			}
			ctl.setElement(element);
			return ctl;
		}
	}
};